package com.trade.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.actions.DispatchAction;

//import com.trade.form.UserForm;
import com.trade.model.Role;
import com.trade.model.User;
import com.trade.service.IRoleService;
import com.trade.service.IUserService;

/**
 * 
 * @author Lin Yang
 * 
 */
public class UserAction extends DispatchAction {
    private static Log log = LogFactory.getLog(UserAction.class);
    private IUserService userService;
    private IRoleService roleService;

    public ActionForward addUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

    	String strUserID = request.getParameter("userid");
        String userName = request.getParameter("loginName").trim();
        String password = request.getParameter("password").trim();
        long roleId = Long.parseLong(request.getParameter("roleId"));
        String name = request.getParameter("name").trim();
        String email = request.getParameter("email").trim();
        String mobilenumber = request.getParameter("mobilenumber");
		mobilenumber = null == mobilenumber ? null : mobilenumber.trim();
        Role role = roleService.getRoleById(roleId);
        
        User user = new User();
        Boolean isSuccess = false;
        if(strUserID==null||strUserID=="")
        {
	        user.setLoginName(userName);
	        user.setPassword(password);
	        user.setRole(role);
	        user.setName(name);
	        user.setEmail(email);
	        user.setMobileNumber(mobilenumber);
	        isSuccess = userService.AddUser(user);
        }
        else
        {
        	user = userService.getUserByUserID(strUserID);
	        user.setLoginName(userName);
	        user.setPassword(password);
	        user.setRole(role);
	        user.setName(name);
	        user.setEmail(email);
	        user.setMobileNumber(mobilenumber);
	        isSuccess = userService.UpdateUser(user);
        }
       
        if (isSuccess) {
        	List<Role> roles = this.roleService.getRoles();
            request.setAttribute("roles", roles);
        	request.setAttribute("failloginName", "成功加入");
            return mapping.findForward("adduser");
        } else {
        	List<Role> roles = this.roleService.getRoles();
            request.setAttribute("roles", roles);
            request.setAttribute("loginName", userName);
            request.setAttribute("failloginName", "失败加入，用户名重复");
        	return mapping.findForward("adduser");
        }

    }
    
    
	public ActionForward GetUsers(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        
        List<User> users = userService.getAllUsers();
        request.setAttribute("users",users);
        return mapping.findForward("manageuser");

    }
    
    public ActionForward DeleteUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

    	String strUserID = request.getParameter("userid");
    	Boolean isSuccess = userService.DeleteUser(strUserID);
    	if(!isSuccess)
    	{
    		return mapping.findForward("manageuser");
    	}
        List<User> users = userService.getAllUsers();
        request.setAttribute("users",users);
        return mapping.findForward("manageuser");
    }

    public ActionForward EditUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	log.debug("edit User...");
    	List<Role> roles = this.roleService.getRoles();
        request.setAttribute("roles", roles);
        String strUserID = request.getParameter("userid");
        User user = userService.getUserByUserID(strUserID);
        request.setAttribute("userid", user.getUserID());
        request.setAttribute("loginName", user.getLoginName());
        request.setAttribute("password", user.getPassword());
        request.setAttribute("name", user.getName());
        request.setAttribute("email", user.getEmail());
        request.setAttribute("mobilenumber", user.getMobileNumber());
        return mapping.findForward("adduser");
    }

	public ActionForward getRole(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	log.debug("roles request...");
    	List<Role> roles = this.roleService.getRoles();
        request.setAttribute("roles", roles);
        request.setAttribute("failloginName", "提交信息提示");
        return mapping.findForward("adduser");
    }
	
	public ActionForward LogOut(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        return mapping.findForward("index");
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }
    
    public IUserService getUserService()
    {
    	return this.userService;
    }

    public void setRoleService(IRoleService roleService) {
        this.roleService = roleService;
    }
    
    public IRoleService getRoleService()
    {
    	return this.roleService;
    }
    
}
